﻿// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");

using Sdk;
using System.CommandLine;

//DocConverter converter = new DocConverter();
string GetInnerExceptionMessage(Exception ex)
{
    if (ex.InnerException != null)
    {
        return GetInnerExceptionMessage(ex.InnerException);
    }
    return ex.Message;
}

var inputFileOption = new Option<FileInfo>("--input", "数据库文档");
inputFileOption.AddAlias("-i");
inputFileOption.IsRequired = true;

var outputFileOption = new Option<FileInfo>("--output", "输出文件");
outputFileOption.AddAlias("-o");
outputFileOption.IsRequired = true;

var dbtypeOption = new Option<string>("--type", "数据库类型(mysql,sqlserver)");
dbtypeOption.AddAlias("-t");
dbtypeOption.FromAmong("mysql", "sqlserver");
dbtypeOption.SetDefaultValue("mysql");

var dbengineOption = new Option<string>("--engine", "数据库引擎类型");
dbengineOption.SetDefaultValue("InnoDB");

var withDropOption = new Option<bool>("--withdrop", "是否编写drop语句");
withDropOption.SetDefaultValue(true);

var docTypeOption = new Option<string>("--doctype", "文档类型");
docTypeOption.SetDefaultValue("docx");
docTypeOption.FromAmong("docx", "markdown");
docTypeOption.IsHidden = true;

var rootCommand = new RootCommand("Docx文件转SQL脚本工具")
{
    inputFileOption,
    outputFileOption,
    dbtypeOption,
    dbengineOption,
    withDropOption
};

rootCommand.SetHandler((opt) =>
{
    try
    {
        var result = opt.ParseResult;
        var inputFile = result.GetValueForOption(inputFileOption);
        var outputFile = result.GetValueForOption(outputFileOption);
        var dbType = result.GetValueForOption(dbtypeOption);
        var dbEngine = result.GetValueForOption(dbengineOption);
        var withDrop = result.GetValueForOption(withDropOption);
        var docType = result.GetValueForOption(docTypeOption);

        if (inputFile == null)
        {
            throw new ArgumentNullException("缺少--input参数");
        }
        if (outputFile == null)
        {
            throw new ArgumentNullException("缺少--output参数");
        }
        //if (outputFile.Exists) 
        //{
        //    outputFile.Delete();
        //}

        DocConverter converter = new DocConverter();
        converter.SetInputFile(inputFile.FullName);
        converter.SetOutputFile(outputFile.FullName);
        converter.WithDrop(withDrop);
        if (!string.IsNullOrWhiteSpace(dbType))
        {
            var t = dbType.Trim().ToLower();
            switch (t)
            {
                case "mysql":
                    converter.SetDbType(DbTypeEnum.MySql);
                    break;
                case "sqlserver":
                    converter.SetDbType(DbTypeEnum.SqlServer);
                    break;
                default:
                    break;
            }
        }
        if (!string.IsNullOrWhiteSpace(dbEngine))
        {
            converter.SetEngine(dbEngine);
        }
        if (!string.IsNullOrWhiteSpace(docType))
        {
            var t = docType.Trim().ToLower();
            switch (t)
            {
                case "docx":
                    converter.SetDocFileType(InputFileTypeEnum.Docx);
                    break;
                case "markdown":
                    converter.SetDocFileType(InputFileTypeEnum.Markdown);
                    break;
                default:
                    break;
            }
        }
        converter.ToSQL();
    }
    catch (Exception ex)
    {
        Console.WriteLine("发生错误:" + GetInnerExceptionMessage(ex));
    }
});

await rootCommand.InvokeAsync(args);


